home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XPAL.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  15KB  |  552 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XPAL
  3. ;
  4. ; Palette functions all MODE X 256 Color resolutions
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ; ****** Aeronautical Research Laboratory              ****************
  14. ; ****** Defence Science and Technology Organisation   ****************
  15. ; ****** Australia                                     ****************
  16. ;
  17. ; egg@dstos3.dsto.gov.au
  18. ; teg@bart.dsto.gov.au
  19. ;-----------------------------------------------------------------------
  20.  
  21.  
  22. COMMENT    $
  23.  
  24.  
  25.  
  26.     All the functions in this module operate on two variations of the
  27.     pallete buffer, the raw and annotated buffers.
  28.  
  29.     All those functions ending in buff operate on the following palette
  30.     structure:
  31.  
  32.        BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  33.  
  34.     No reference to the starting colour index or number of colours stored
  35.     is contained in the structure.
  36.  
  37.     All those functions ending in struc operate on the following palette
  38.     structure:
  39.  
  40.        BYTE:c,BYTE:n,BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  41.  
  42.     where c is the starting colour and n is the number of colours stored
  43.  
  44. $
  45.  
  46.  
  47.  
  48.  
  49. include xlib.inc
  50. include xpal.inc
  51.  
  52. .code
  53.  
  54.  
  55. ;----------------------------------------------------------------------
  56. ; Read DAC palette into annotated type buffer with interrupts disabled
  57. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  58. ;
  59. ; x_get_pal_struc(char far * pal, int num_colrs, int start_color)
  60. ;
  61. ; WARNING: memory for the palette buffers must all be pre-allocated
  62. ;
  63. ; Written by Themie Gouthas
  64. ;----------------------------------------------------------------------
  65. _x_get_pal_struc  proc
  66. ARG  PalBuff:dword,NumColors:word,StartColor:word
  67.      push  bp        ; Set up stack frame
  68.      mov   bp,sp
  69.      push  di
  70.      push  si
  71.      cld
  72.  
  73.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  74.      mov   si,[StartColor]         ; Store the Start Colour
  75.      mov   ax,si
  76.      stosb
  77.      mov   dx,[NumColors]          ; Store the Number of Colours
  78.      mov   al,dl
  79.      stosb
  80.  
  81.      mov   cx,dx                   ; setup regs and jump
  82.      jmp   short ReadPalEntry
  83.  
  84. _x_get_pal_struc endp
  85.  
  86.  
  87.  
  88.  
  89.  
  90. ;----------------------------------------------------------------------
  91. ; Read DAC palette into raw buffer with interrupts disabled
  92. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  93. ;
  94. ; x_get_pal_raw(char far * pal, int num_colrs, int start_index)
  95. ;
  96. ; WARNING: memory for the palette buffers must all be pre-allocated
  97. ;
  98. ; Written by Themie Gouthas
  99. ;----------------------------------------------------------------------
  100. _x_get_pal_raw    proc
  101. ARG  PalBuff:dword,NumColors:word,StartColor:word
  102.      push  bp        ; Set up stack frame
  103.      mov   bp,sp
  104.      push  di
  105.      push  si
  106.  
  107.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  108.  
  109.      mov  si,[StartColor]
  110.      mov  cx,[NumColors]
  111.  
  112. ReadPalEntry:
  113.      cld
  114.      WaitVsyncStart
  115.      mov  ax,si
  116.      mov  dx,DAC_READ_INDEX
  117.      cli
  118.      out  dx,al                    ; Tell DAC what colour to start reading
  119.      mov  dx,DAC_DATA
  120.  
  121.      mov  bx,cx                    ; set cx to Num Colors * 3 ( size of
  122.      shl  bx,1                     ; palette buffer)
  123.      add  cx,bx
  124.  
  125.      rep  insb                     ; read the palette enntries
  126.  
  127.      sti
  128.      pop  si
  129.      pop  di
  130.      pop  bp
  131.      ret
  132. _x_get_pal_raw endp
  133.  
  134.  
  135.  
  136. ;----------------------------------------------------------------------
  137. ; Write DAC palette from annotated type buffer with interrupts disabled
  138. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  139. ;
  140. ; x_put_pal_struc(char far * pal)
  141. ;
  142. ; Written by Themie Gouthas
  143. ;----------------------------------------------------------------------
  144.  
  145. _x_put_pal_struc proc
  146. ARG     CompPalBuff:dword
  147.         push    bp      ;preserve caller's stack frame
  148.     mov     bp,sp   ;point to local stack frame
  149.     push    ds
  150.     push    si
  151.     cld
  152.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  153.     lodsb               ; get the colours to skip
  154.     mov     ah,0
  155.     mov     bx,ax              ; skip colours
  156.  
  157.     lodsb               ; get the count of colours to set
  158.     mov     ah,0
  159.     mov    cx,ax              ; use it as a loop counter
  160.     jmp     short WritePalEntry
  161.  
  162. _x_put_pal_struc  endp
  163.  
  164.  
  165. ;----------------------------------------------------------------------
  166. ; Write DAC palette from annotated type buffer with interrupts disabled
  167. ; starting at a new palette index
  168. ;
  169. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  170. ;
  171. ; x_transpose_pal_struc(char far * pal, int StartColor)
  172. ;
  173. ; WARNING: memory for the palette buffers must all be pre-allocated
  174. ;
  175. ; Written by Themie Gouthas
  176. ;----------------------------------------------------------------------
  177.  
  178. _x_transpose_pal_struc proc
  179. ARG     CompPalBuff:dword,StartColor:word
  180.         push    bp      ;preserve caller's stack frame
  181.     mov     bp,sp   ;point to local stack frame
  182.     push    ds
  183.     push    si
  184.     cld
  185.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  186.     mov     bx,[StartColor]
  187.     mov     [si],bl
  188.     inc     si
  189.     lodsb               ; get the count of colours to set
  190.     mov     ah,0
  191.     mov    cx,ax              ; use it as a loop counter
  192.     jmp     short WritePalEntry
  193. _x_transpose_pal_struc endp
  194.  
  195.  
  196. ;----------------------------------------------------------------------
  197. ; Write DAC palette from raw buffer with interrupts disabled
  198. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  199. ;
  200. ; _x_put_pal_raw(char far * pal, int num_colrs, int start_index)
  201. ;
  202. ; Written by Themie Gouthas
  203. ;----------------------------------------------------------------------
  204. _x_put_pal_raw  proc
  205. ARG  PalBuff:dword,NumColors:word,StartColor:word
  206.         push bp        ; Set up stack frame
  207.         mov  bp,sp
  208.         push ds
  209.         push si
  210.  
  211.         mov  cx,[NumColors]      ; Number of colours to set
  212.         mov  bx,[StartColor]
  213.         lds  si,[PalBuff]        ; ds:si -> palette buffer
  214.  
  215. WritePalEntry:
  216.         or   cx,cx
  217.         jz   @@Done
  218.         cli
  219.         cld                      ; Make sure we're going the right way
  220.         WaitVsyncStart           ; Wait for vert sync to start
  221.                 mov  ax,bx
  222.         mov  bx,60               ; set the vsync check timer (Vsync
  223.                              ; is tested for at each bx'th entry to
  224.                      ; prevent snow 60 is otimum for 10
  225.                      ; MHz 286 or greater
  226.  
  227. @@SetLoop:
  228.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  229.         out  dx,al               ; writing from
  230.         mov  dx,DAC_DATA
  231.  
  232.         outsb                    ; Set the red component
  233.         outsb                    ; Set the green component
  234.         outsb                    ; Set the blue component
  235.         inc  al                  ; increment the colour index
  236.         dec  bx                  ; decrement vsync test counter
  237.         js   @@test_vsync        ; ready to test for vsync again ?
  238.         loop @@SetLoop           ; No! - continue loop
  239.         jmp  short @@Done        ; All colours done
  240.  
  241. @@test_vsync:
  242.         mov     dx,INPUT_STATUS_0
  243.         push    ax               ; save current colour index
  244. @@Wait:
  245.         in      al,dx            ; wait for vsync leading edge pulse
  246.         test    al,08h
  247.         jz      @@Wait
  248.  
  249.         pop     ax               ; restore current colour index
  250.         mov     bx,60            ; reset vsync test counter
  251.         loop @@SetLoop           ; loop for next colour index
  252.  
  253. @@Done:
  254.         sti
  255.         pop  si
  256.         pop  ds
  257.         pop  bp
  258.         ret
  259. _x_put_pal_raw endp
  260.  
  261.  
  262.  
  263. ;----------------------------------------------------------------------
  264. ; Set the RGB setting of a vga color
  265. ;
  266. ; _x_set_rgb(unsigned char color, unsigned char R,unsigned char G,
  267. ;            unsigned char B)
  268. ;
  269. ;
  270. ; Written by Themie Gouthas
  271. ;----------------------------------------------------------------------
  272. _x_set_rgb  proc
  273. ARG  ColorIndex:byte,R:byte,G:byte,B:byte
  274.         push bp        ; Set up stack frame
  275.         mov  bp,sp
  276.  
  277.         mov  al,[ColorIndex]
  278.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to
  279.         out  dx,al               ; write to
  280.         mov  dx,DAC_DATA
  281.  
  282.         mov  al,[R]              ; Set the red component
  283.         out  dx,al
  284.         mov  al,[G]              ; Set the green component
  285.         out  dx,al
  286.         mov  al,[B]              ; Set the blue component
  287.         out  dx,al
  288.         pop  bp
  289.         ret
  290. _x_set_rgb endp
  291.  
  292.  
  293. ;----------------------------------------------------------------------
  294. ; Rotate annotated palette buffer entries
  295. ;
  296. ; x_rot_pal_struc(char far * pal, int direction)
  297. ;
  298. ; Direction : 0 = backward 1 = forward
  299. ;
  300. ; Written by Themie Gouthas
  301. ;----------------------------------------------------------------------
  302. _x_rot_pal_struc  proc
  303. ARG  PalBuff:dword,Direction:word
  304.     push bp        ; Set up stack frame
  305.     mov     bp,sp
  306.     push ds
  307.     push si
  308.     push di
  309.  
  310.     cld
  311.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  312.     lodsw         ; al = colorst ot skip, ah = num colors
  313.  
  314.     xor  ch,ch    ; Set the number of palette entries to cycle in cx
  315.     mov  cl,ah    ;
  316.  
  317.     jmp  short RotatePalEntry
  318.  
  319. _x_rot_pal_struc  endp
  320.  
  321.  
  322.  
  323. ;----------------------------------------------------------------------
  324. ; Rotate raw palette buffer
  325. ;
  326. ; x_rot_pal_raw(char far * pal, int direction, int num_colrs)
  327. ;
  328. ; Direcction : 0 = backward 1 = forward
  329. ;
  330. ; Written by Themie Gouthas
  331. ;----------------------------------------------------------------------
  332. _x_rot_pal_raw    proc
  333. ARG  PalBuff:dword,Direction:word,NumColors:word
  334.     push bp        ; Set up stack frame
  335.     mov     bp,sp
  336.     push ds
  337.     push si
  338.     push di
  339.  
  340.     cld
  341.     mov     cx,[NumColors]          ; Set the number of palette entries to cycle
  342.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  343.  
  344. RotatePalEntry:
  345.  
  346.  
  347.     mov     ax,ds                ; copy ds to es
  348.     mov     es,ax
  349.  
  350.     dec  cx
  351.     mov     bx,cx                ; Multiply cx by 3
  352.     shl     bx,1
  353.     add  cx,bx
  354.  
  355.     cmp  [Direction],0        ; are we going forward ?
  356.     jne  @@forward            ; yes - jump (colors move one position back)
  357.  
  358.     std                       ; no - set reverse direction
  359.     add  si,cx                ; set si to last byte in palette
  360.     add  si,2
  361.  
  362. @@forward:
  363.     mov     ax,si                ; copy si to di
  364.     mov     di,ax
  365.  
  366.     lodsb                     ; load first color triplet into regs
  367.     mov  dl,al
  368.     lodsb
  369.     mov  dh,al
  370.     lodsb
  371.     mov  bl,al
  372.  
  373.     rep     movsb                ; move remaining triplets direction indicated
  374.                   ; by direction flag
  375.  
  376.     mov  al,dl                ; write color triplet from regs to last position
  377.     stosb
  378.     mov  al,dh
  379.     stosb
  380.     mov  al,bl
  381.     stosb
  382.  
  383.     pop     di
  384.     pop     si
  385.     pop     ds
  386.     pop     bp
  387.     ret
  388. _x_rot_pal_raw  endp
  389.  
  390. ;----------------------------------------------------------------------
  391. ; Copy palette making intensity adjustment
  392. ; x_cpcontrast_pal_struc(char far *src_pal, char far *dest_pal, unsigned char Intensity)
  393. ;
  394. ; WARNING: memory for the palette buffers must all be pre-allocated
  395. ;
  396. ; Written by Themie Gouthas
  397. ;----------------------------------------------------------------------
  398. _x_cpcontrast_pal_struc    proc
  399. ARG  PalSrcBuff:dword,PalDestBuff:dword,Intensity:byte
  400.     push bp        ; Set up stack frame
  401.     mov     bp,sp
  402.     push ds
  403.     push si
  404.     push di
  405.  
  406.     cld
  407.     mov  bh,0ffh
  408.     sub  bh,[Intensity]
  409.     and  bh,07fh            ; Palettes are 7 bit
  410.     lds     si,dword ptr [PalSrcBuff]  ; point ds:si to Source Palette buffer
  411.     les     di,dword ptr [PalDestBuff] ; point ds:si to Source Palette buffer
  412.     lodsw                ; al = colorst ot skip, ah = num color
  413.     stosw
  414.  
  415.     xor  ch,ch    ; Set the number of palette entries to adjust
  416.     mov  cl,ah    ;
  417.  
  418.     mov  dx,0     ; flag set to 0 if all output palette entries zero
  419. @@MainLoop:
  420.     lodsw
  421.     sub  al,bh               ; adjust intensity and copy RED
  422.     jns  @@DecrementOK_R
  423.     xor  al,al
  424. @@DecrementOK_R:
  425.     sub  ah,bh               ; adjust intensity and copy GREEN
  426.     jns  @@DecrementOK_G
  427.     xor  ah,ah
  428. @@DecrementOK_G:
  429.     or   dx,ax
  430.     or   dl,ah
  431.     stosw
  432.     lodsb
  433.     sub  al,bh               ; adjust intensity and copy BLUE
  434.     jns  @@DecrementOK_B
  435.     xor  al,al
  436. @@DecrementOK_B:
  437.     or   dl,al
  438.     stosb
  439.     loop @@MainLoop
  440.  
  441.     mov  ax,dx
  442.     pop  di
  443.     pop     si
  444.     pop     ds
  445.     pop     bp
  446.     ret
  447. _x_cpcontrast_pal_struc  endp
  448.  
  449.  
  450.  
  451. ;----------------------------------------------------------------------
  452. ; Write DAC palette from annotated type buffer with specified intensity
  453. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  454. ;
  455. ; x_put_contrast_pal_struc(char far * pal, unsigned char  intensity)
  456. ;
  457. ; Designed for fading in or out a palette without using an intermediate
  458. ; working palette buffer ! (Slow but memory efficient ... OK for small
  459. ; pal strucs}
  460. ;
  461. ; Written by Themie Gouthas
  462. ;----------------------------------------------------------------------
  463.  
  464. _x_put_contrast_pal_struc proc
  465. ARG     CompPalBuff:dword,Intensity:byte
  466.         push    bp      ;preserve caller's stack frame
  467.     mov     bp,sp   ;point to local stack frame
  468.     push    ds
  469.     push    si
  470.     push    di
  471.     cld
  472.  
  473.     mov     bh,0ffh
  474.     sub     bh,[Intensity]
  475.     and     bh,07fh            ; Palettes are 7 bit
  476.     mov     di,40              ; set the vsync check timer (Vsync
  477.                    ; is tested for at each di'th entry to
  478.                    ; prevent snow 40 is otimum for 10
  479.                    ; MHz 286 or greater)
  480.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  481.     lodsb               ; get the colours to skip
  482.     mov     bl,al
  483.  
  484.     lodsb               ; get the count of colours to set
  485.     mov     ah,0
  486.     mov    cx,ax              ; use it as a loop counter
  487.     or      cx,cx
  488.     jz      @@Done
  489.  
  490.     WaitVsyncStart           ; Wait for vert sync to start
  491.  
  492. @@MainLoop:
  493.         mov  al,bl
  494.     mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  495.     out  dx,al               ; writing from
  496.     inc  dx                  ; == mov  dx,DAC_DATA
  497.  
  498.     lodsb                    ; Load each colour component, modify for
  499.     sub  al,bh               ; intensity and write to DAC H/Ware
  500.     jns  @@DecrementOK_R
  501.     xor  al,al
  502. @@DecrementOK_R:
  503.     out  dx,al
  504.  
  505.     lodsb
  506.     sub  al,bh
  507.     jns  @@DecrementOK_G
  508.     xor  al,al
  509. @@DecrementOK_G:
  510.     out  dx,al
  511.  
  512.     lodsb
  513.     sub  al,bh
  514.     jns  @@DecrementOK_B
  515.     xor  al,al
  516. @@DecrementOK_B:
  517.     out  dx,al
  518.  
  519.     inc  bl                  ; increment color index
  520.     dec  di                  ; decrement vsync test flag
  521.     js   @@test_vsync
  522.     loop @@MainLoop
  523.     jmp  short @@Done
  524.  
  525.  
  526. @@test_vsync:
  527.     mov     dx,INPUT_STATUS_0
  528.     push    ax               ; save current colour index
  529. @@Wait:
  530.     in      al,dx            ; wait for vsync leading edge pulse
  531.     test    al,08h
  532.     jz      @@Wait
  533.  
  534.     pop     ax               ; restore current colour index
  535.     mov     di,40            ; reset vsync test counter
  536.     loop    @@MainLoop       ; loop for next colour index
  537.  
  538. @@Done:
  539.     sti
  540.     pop  di
  541.     pop  si
  542.     pop  ds
  543.     pop  bp
  544.     ret
  545.  
  546. _x_put_contrast_pal_struc   endp
  547.  
  548.  
  549.     end
  550.  
  551.  
  552.